﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using iMapa.UI.ModelView.Main;
using iMapa.UI.View.Simulation;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using iMapa.Backend;

namespace iMapa.UI.View.Main
{
    /// <summary>
    /// Interaction logic for MapGroupListItem.xaml
    /// </summary>
    public partial class MapGroupListItem : UserControl
    {


        public MapGroupListItem()
        {
            InitializeComponent();
            
        }


        private void nameEntered(object sender, RoutedEventArgs e)
        {
            ((GroupViewModel)this.DataContext).IsNew = false;
        }

        private void onNameEnter(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Enter))
            {
                ((GroupViewModel)this.DataContext).IsNew = false;
            }
        }

        private void selectAllTextKB(object sender, KeyboardFocusChangedEventArgs e)
        {
            selectAllText(sender,e);
        }

        private void selectAllText(object sender,RoutedEventArgs e)
        {
            var textBox = e.OriginalSource as TextBox;
            if (textBox != null)
                textBox.SelectAll();
        }

        private void selectAllTextInit(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null)
                textBox.SelectAll();
        }

        private void onRename(object sender, RoutedEventArgs e)
        {
            ((GroupViewModel)this.DataContext).IsNew = true;
        }

        private void onDelete(object sender, RoutedEventArgs e)
        {
            GroupViewModel m=(GroupViewModel)this.DataContext;
            if (m.Maps.Count == 0)
            {
                m.Parent.removeGroup(m);
            }
        }

        private void SimulateButton_Click(object sender, RoutedEventArgs e)
        {
            GroupViewModel model=((GroupViewModel)this.DataContext);
            model.Root.CurrentViewObject = model;

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GroupViewModel model = (GroupViewModel)this.DataContext;
            model.Maps.CollectionChanged += UpdateBackend;
        }

        private void UpdateBackend(object sender, NotifyCollectionChangedEventArgs e)
        {
            GroupViewModel model = (GroupViewModel)this.DataContext;
            ObservableCollection<MapViewModel> list = (ObservableCollection<MapViewModel>)sender;
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (MapViewModel m in e.NewItems)
                {
                    m.Imap.ParentGroup = model.Name;
                    model.Igroup.Maps.Add(m.Imap);
                    InteractMapLoader.Save(m.Imap);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (MapViewModel m in e.OldItems)
                {
                    model.Igroup.Maps.Remove(m.Imap);
                }
            }
        }
    }
}
